package com.dlry.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dlry.entity.DlLoginuser;
import com.dlry.entity.DlMeetingRoom;
import com.dlry.entity.DlMeetingRoomBooking;
import com.dlry.entity.DlNotification;
import com.dlry.entity.DlShare;
import com.dlry.serviceImpl.MeetingRoomService;
import com.dlry.serviceImpl.MeetingRoomServiceImpl;
import com.dlry.serviceImpl.NotificationService;
import com.dlry.serviceImpl.NotificationServiceImpl;
import com.dlry.serviceImpl.ShareService;
import com.dlry.serviceImpl.UserService;
import com.dlry.serviceImpl.UserServiceImpl;

@Controller
@RequestMapping({ "/", "/homepage" })

public class MainController extends BaseController {
	@Autowired
	private NotificationService notificationService;
	@Autowired
	private MeetingRoomService meetingRoomService;
	@Autowired
	private UserService userService;
	@Autowired
	private ShareService shareService;
	@Autowired
	private EhCacheCacheManager cacheManager;

	static final Logger LOGGER = LogManager.getFormatterLogger("login_log");

	public MainController() {
	}

	public MainController(NotificationServiceImpl notificationService, MeetingRoomServiceImpl meetingRoomService,
			UserServiceImpl userService) {
		this.userService = userService;
		this.meetingRoomService = meetingRoomService;
		this.notificationService = notificationService;
	}

	@RequestMapping("/error")
	public String name() {
		throw new IllegalArgumentException("测试错误");
	}

	@RequestMapping(method = RequestMethod.GET)

	public String homePage(Model model, HttpSession session, String logined) throws Exception {
		String username = getPrincipal();
		if (logined != null && username != null) {
			LOGGER.info(username + " logined");
		}
		// List<DlNotification> ln= new ArrayList<DlNotification>();
		Page<DlNotification> notifications = notificationService.findAllPage(0, 15);
		List<DlMeetingRoom> rooms = meetingRoomService.finaAll();
		List<DlShare> shares = shareService.findAll();
		// Timestamp now = new Timestamp(new Date().getTime());
		Map<Long, List<DlMeetingRoomBooking>> roomBookingMap = new HashMap<Long, List<DlMeetingRoomBooking>>();
		// LoadRoom loadRoom = new LoadRoom(model, rooms);
		// LoadNotification loadNotification = new LoadNotification(model,
		// notifications);
		// LoadBookingMap loadBookingMap = new LoadBookingMap(roomBookingMap,
		// model, rooms);
		// Thread t1 = new Thread(loadNotification);
		// Thread t2 = new Thread(loadRoom);
		// Thread t3 = new Thread(loadBookingMap);
		// t1.start();
		// t2.start();
		// t3.start();
		Timestamp now = new Timestamp(new Date().getTime());
		for (DlMeetingRoom room : rooms) {
			List<DlMeetingRoomBooking> bookings = meetingRoomService.getBookingsByIdFrom(room.getId(), now);
			roomBookingMap.put(room.getId(), bookings);
		}
		model.addAttribute("rooms", rooms);
		model.addAttribute("notifications", notifications.getContent());
		model.addAttribute("roomBookingMap", roomBookingMap);
		model.addAttribute("shares", shares);
		String name = getPrincipal();
		if (name != null) {
			DlLoginuser duser = userService.findByName(name);
			model.addAttribute("duser", duser);
			session.setAttribute("user", duser);
		}
		model.addAttribute("title", "动力燃油系统设计研究部");
		// LOGGER.error("error messager");
		// if (true) {
		// throw new EjbAccessException("test");
		// }

		Cache cache = cacheManager.getCache("main");
		System.out.println(cache.get("main"));

		return "main";
		// FutureTask<String> futureTask = new FutureTask<String>(
		// new ThreadHomePageReturn(notifications, rooms, roomBookingMap));
		// Thread t4 = new Thread(futureTask);
		// t4.start();
		// return futureTask.get();
	}

	// // 待返回至的线程
	// class ThreadHomePageReturn implements Callable<String> {
	// private Page<DlNotification> notifications;
	// private List<DlMeetingRoom> rooms;
	// Map<Long, List<DlMeetingRoomBooking>> roomBookingMap;
	//
	// public ThreadHomePageReturn(Page<DlNotification> notifications,
	// List<DlMeetingRoom> rooms,
	// Map<Long, List<DlMeetingRoomBooking>> roomBookingMap) {
	// this.rooms = rooms;
	// this.notifications = notifications;
	// this.roomBookingMap = roomBookingMap;
	// }
	//
	// public String call() throws Exception {
	// // 获取所有锁才可以进行
	// synchronized (notifications) {
	// synchronized (rooms) {
	// synchronized (roomBookingMap) {
	// return "main";
	// }
	// }
	// }
	// }
	//
	// }
	//
	// // 新建线程获取notification数组
	// class LoadNotification implements Runnable {
	// private Model model;
	// private Page<DlNotification> notifications;
	//
	// public LoadNotification(Model model, Page<DlNotification> notifications)
	// {
	// this.model = model;
	// this.notifications = notifications;
	// }
	//
	// public void run() {
	// // 获取notifications对象锁
	// synchronized (notifications) {
	// notifications = notificationService.findAllPage(0, 15);
	// model.addAttribute("notifications", notifications.getContent());
	// }
	// }
	//
	// }
	//
	// // 新建线程获取room
	// class LoadRoom implements Runnable {
	// private Model model;
	// private List<DlMeetingRoom> rooms;
	//
	// public LoadRoom(Model model, List<DlMeetingRoom> rooms) {
	// this.model = model;
	// this.rooms = rooms;
	// }
	//
	// public void run() {
	// synchronized (rooms) {
	// rooms = meetingRoomService.finaAll();
	// model.addAttribute("rooms", rooms);
	// }
	//
	// }
	// }
	//
	// // 新建线程获取Map<Long, List<DlMeetingRoomBooking>> roomBookingMap
	// class LoadBookingMap implements Runnable {
	// private Map<Long, List<DlMeetingRoomBooking>> roomBookingMap;
	// private List<DlMeetingRoom> rooms;
	// private Model model;
	//
	// public LoadBookingMap(Map<Long, List<DlMeetingRoomBooking>>
	// roomBookingMap, Model mode,
	// List<DlMeetingRoom> rooms) {
	// this.roomBookingMap = roomBookingMap;
	// this.model = mode;
	// this.rooms = rooms;
	// }
	//
	// public void run() {
	// synchronized (rooms) {
	// synchronized (roomBookingMap) {
	// for (DlMeetingRoom room : rooms) {
	// Timestamp now = new Timestamp(new Date().getTime());
	// List<DlMeetingRoomBooking> bookings =
	// meetingRoomService.getBookingsByIdFrom(room.getId(), now);
	// roomBookingMap.put(room.getId(), bookings);
	// }
	// model.addAttribute("roomBookingMap", roomBookingMap);
	// }
	// }
	//
	// }
	//
	// }

}
